package ui.panels;

import entities.Holiday;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.GenericDriver;
import model.ViewHolidayTableModel;
import ui.windows.RemedySchedulerFrame;
import ui.windows.ViewHolidayDialog;

public class ViewHolidayPanel extends javax.swing.JPanel {

    /** Creates new form ViewHolidayPanel */
    public ViewHolidayPanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFrame = mainFrame;
        includeModel();
    }

    private void includeModel() {
        GenericDriver driver = mainFrame.getGenericDriver();
        model = new ViewHolidayTableModel(driver.showHoliday());
        holidayTable.setModel(model);
        holidayTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        holidayTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer.setBackground(new Color(100, 50, 102));
        cellRenderer.setForeground(Color.white);
        holidayTable.getTableHeader().setReorderingAllowed(false);
        holidayTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        holidayTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        holidayTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        holidayTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
        holidayTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        holidayTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer);
        holidayTable.getColumnModel().getColumn(3).setPreferredWidth(360);
        holidayTable.setFillsViewportHeight(true);
    }

    private void setSelectedRecord(java.awt.event.MouseEvent evt) {
        int index = holidayTable.getSelectedRow();
        if (index != -1) {
            selectedRecord = model.getAt(index);
            if (selectedRecord != null) {
                if(evt.getClickCount() == 2) {
                    ViewHolidayDialog dialog = new ViewHolidayDialog(mainFrame);
                    dialog.setLocationRelativeTo(null);
                    dialog.setSelectedHoliday(selectedRecord);
                    dialog.setVisible(true);
                }
            }
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private ViewHolidayTableModel model;
    private Holiday selectedRecord;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        View = new javax.swing.JScrollPane();
        holidayTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));

        View.setBackground(new java.awt.Color(255, 255, 255));
        View.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Holiday", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        holidayTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        holidayTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                holidayTableMouseClicked(evt);
            }
        });
        View.setViewportView(holidayTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(View, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(View, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void holidayTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_holidayTableMouseClicked
    setSelectedRecord(evt);
}//GEN-LAST:event_holidayTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane View;
    private javax.swing.JTable holidayTable;
    // End of variables declaration//GEN-END:variables
}
